package com.demo.common.exception;

import com.demo.common.enums.REnum;
import com.demo.common.base.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 异常处理器
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestControllerAdvice
@Slf4j
public class RRExceptionHandler {
    /**
     * 处理自定义异常
     */
    @ExceptionHandler(RRException.class)
    public R handleRRException(RRException e) {
        log.error(e.getMessage());
        return R.error(e.getCode(), e.getMessage()).desc(e.getDesc());
    }

    /**
     * 警告异常处理
     */
    @ExceptionHandler(AlertException.class)
    public R handleAlertException(AlertException e) {
        log.warn(e.getMsg());
        return R.warn(e.getMsg(), e.getNames());
    }

    /**
     * 需要登录
     */
    @ExceptionHandler(UnauthenticatedException.class)
    public R handleUnauthenticatedException(UnauthenticatedException e) {
        return R.needLogin();
    }

    /**
     * 需要授权
     */
    @ExceptionHandler(UnauthorizedException.class)
    public R handleUnauthorizedException(UnauthorizedException e) {
        return R.needPermit();
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public R handlerNoFoundException(Exception e) {
        log.error(e.getMessage());
        return R.error(REnum.NOT_FOUND.getCode(), "路径不存在，请检查路径是否正确").desc(e.getMessage());
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public R handleDuplicateKeyException(DuplicateKeyException e) {
        log.error(e.getMessage());
        return R.error("数据库中已存在该记录").desc(e.getMessage());
    }

    @ExceptionHandler(UnknownAccountException.class)
    public R handleUnknownAccountException(UnknownAccountException e) {
        log.error(e.getMessage());
        return R.error(REnum.UN_LOGIN.getCode(), "用户不存在").desc(e.getMessage());
    }

    @ExceptionHandler(IncorrectCredentialsException.class)
    public R handleIncorrectCredentialsException(IncorrectCredentialsException e) {
        log.error(e.getMessage());
        return R.error(REnum.UN_LOGIN.getCode(), "账号或密码错误").desc(e.getMessage());
    }

    @ExceptionHandler(LockedAccountException.class)
    public R handleLockedAccountException(LockedAccountException e) {
        log.error(e.getMessage());
        return R.error(REnum.UN_LOGIN.getCode(), "账号已被锁定,请联系管理员").desc(e.getMessage());
    }

    @ExceptionHandler(AuthorizationException.class)
    public R handleAuthorizationException(AuthorizationException e) {
        log.error(e.getMessage());
        return R.error(REnum.UN_AUTH.getCode(), "没有权限，请联系管理员授权").desc(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public R handleException(Exception e) {
        log.error(e.getMessage(), e);
        return R.error().desc(e.getMessage());
    }
}
